package cn.sxw.ld.module.image.controller;

import cn.sxw.ld.common.dto.R;
import cn.sxw.ld.common.utils.CommonUtil;
import cn.sxw.ld.module.image.pojo.Image;
import cn.sxw.ld.module.image.service.ImageService;
import cn.sxw.ld.module.scrolling.pojo.Scrolling;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:ImageController
 * Package:cn.sjxy.graduation.controller
 * Description:
 *
 * @Date:2022/6/25 14:29
 * @Author:SXW
 */
@Slf4j
@RestController
@RequestMapping("/image")
public class ImageController {

    @Autowired
    private ImageService imageService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        Page pageInfo = imageService.getPage(page, pageSize);
        return R.success(pageInfo);
    }

    @PostMapping("/add")
    public R<String> addScrolling(@RequestBody Image image) {
        imageService.save(image);
        return R.success("添加成功");
    }

    @DeleteMapping()
    public R<String> deleteScrolling(Long id) {
        imageService.removeById(id);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<Image>> list() {
        List<Image> list = imageService.list();
        int size = list.size();
        if (size < 10) {
            return R.success(list);
        }else {
            ArrayList<Image> images = new ArrayList<>();
            List<Integer> randomInt = CommonUtil.getRandomInt(10, 0, size - 1);
            for (Integer integer : randomInt) {
                images.add(list.get(integer));
            }
            return R.success(images);
        }
    }


}
